package com.sr.examination.exam.exercise.category;

import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sr.examination.R;
import com.sr.examination.exam.exercise.category.adapter.ChoiceAdapter;
import com.tl.commonlibrary.ui.beans.ExerciseOption;

import java.util.ArrayList;

/**
 * Created by jiangzhongshan on 18/2/3.
 * 选择题（单选SingleChoice or 多选MultipleChoice or 判断JudgementChoice or 材料题）
 */

public abstract class ChoiceFragment extends ExerciseFragment {

    protected ListView choiceListView;
    protected ChoiceAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frag_choice_exercise, null);
        choiceListView = root.findViewById(R.id.choiceListView);
        super.onCreateView(inflater, container, savedInstanceState);
        init();
        return root;
    }

    protected abstract void initOption();

    @Override
    public void lazyLoad() {
        super.lazyLoad();
    }

    protected void init() {
        initOption();
        adapter = new ChoiceAdapter(context, exerciseBean.getOptions());
        choiceListView.setAdapter(adapter);
//        choiceListView.setItemChecked(exerciseBean.getOptions().size()-1,true);
        choiceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<ExerciseOption> options = new ArrayList<>();
                if (choiceListView.getChoiceMode() == AbsListView.CHOICE_MODE_MULTIPLE) {
                    SparseBooleanArray booleanArray = choiceListView.getCheckedItemPositions();
                    StringBuilder myAnswer = new StringBuilder();
                    for (int i = 0; i < booleanArray.size(); i++) {
                        int pos = booleanArray.keyAt(i);
                        ExerciseOption option = adapter.getItem(pos);
                        option.setChecked(booleanArray.get(pos));
                        if (option.isChecked()) {
                            options.add(option);
                            myAnswer.append(option.getId() + ",");
                        }
                    }
                    if (myAnswer.length() > 0) {
                        exerciseBean.setUserAnswer(myAnswer.toString().substring(0, myAnswer.length() - 1));
                    }
                    onClickOption(options);
                } else if (choiceListView.getChoiceMode() == AbsListView.CHOICE_MODE_SINGLE) {
                    int pos = choiceListView.getCheckedItemPosition();
                    if (pos >= 0 && pos < adapter.getCount()) {
                        ExerciseOption option = adapter.getItem(pos);
                        option.setChecked(true);
                        options.add(option);
                        exerciseBean.setUserAnswer(option.getId());
                        for (int i = 0; i < adapter.getCount(); i++) {
                            if (pos != i) {
                                adapter.getItem(i).setChecked(false);
                            }
                        }
                        onClickOption(options);
                    }
                }
            }
        });
    }

    @Override
    protected void onExerciseAnsweringModeCalled() {
        super.onExerciseAnsweringModeCalled();
    }

    @Override
    protected void onExerciseAnalysisModeCalled() {
        super.onExerciseAnalysisModeCalled();
        myAnswerTView.setText("我的答案" + exerciseBean.getUserAnswer());
        setAnswer("正确答案" + exerciseBean.getAnswer(), false);
    }

    @Override
    public void saveExerciseInstanceState() {
        super.saveExerciseInstanceState();
    }

    /**
     * 禁止修改答案
     */
    @Override
    public boolean forbiddenAnswer() {
        if (super.forbiddenAnswer()) {
            if (adapter != null) {
                adapter.setCheckable(false);
            }
            choiceListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
            return true;
        }
        return false;
    }

}
